<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="岗位编码" prop="postCode">
        <el-input
          v-model="queryParams.postCode"
          placeholder="请输入岗位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" @click="handleUpdate"
          v-hasPermi="['system:post:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleDelete"
          v-hasPermi="['system:post:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="商品编号" align="center" prop="id" /> -->
      <el-table-column label="奖品名称" align="center" prop="name" />
      <el-table-column label="奖品类型" align="center" prop="prizeType">
        <template slot-scope="scope">
          {{ scope.row.prizeType === 1 ? '测试' : scope.row.prizeType === 2 ? '积分' : scope.row.prizeType === 3 ? '实物' :
            '优惠券' }}
        </template>
      </el-table-column>
      <el-table-column label="奖池等级" align="center" prop="prizeLevel">
        <template slot-scope="scope">
          {{ scope.row.prizeLevel === 1 ? '青铜' : scope.row.prizeLevel === 2 ? '黄金' : '钻石' }}
        </template>
      </el-table-column>
      <el-table-column label="奖品金额" align="center" prop="prizeAmount" />
      <el-table-column label="奖品描述" align="center" prop="description" />
      <el-table-column label="奖品图片" align="center" prop="imageUrl">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.imageUrl"
            :preview-src-list="[scope.row.imageUrl]"></el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column label="上架状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" style="color: red;" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" /> -->

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="奖品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入奖品名称" />
        </el-form-item>
        <el-form-item label="奖品描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入奖品描述" />
        </el-form-item>
        <el-form-item label="奖品类型" prop="prizeType">
          <el-radio-group v-model="form.prizeType">
            <el-radio :label="1">测试</el-radio>
            <el-radio :label="2">积分</el-radio>
            <el-radio :label="3">实物</el-radio>
            <el-radio :label="4">优惠券</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="奖池等级" prop="prizeLevel">
          <el-radio-group v-model="form.prizeLevel">
            <el-radio :label="1">青铜</el-radio>
            <el-radio :label="2">黄金</el-radio>
            <el-radio :label="3">钻石</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="奖品金额" prop="prizeAmount">
          <el-input v-model="form.prizeAmount" placeholder="请输入奖品金额" />
        </el-form-item>
        <el-form-item label="中奖概率" prop="winningProbability">
          <el-input v-model="form.winningProbability" type="number" placeholder="请输入中奖概率最大值为100" />
        </el-form-item>

        <el-form-item label="奖品图片" prop="imageUrl">
          <!-- <el-input v-model="form.imageUrl" :disabled="form.id" placeholder="请输入商品图片地址" /> -->
          <FileUpload ref="FileUpload" @inputFile="FileUploadValChange" :value="FileUploadVal" :limit="1"
            :fileType="fileType">
          </FileUpload>
        </el-form-item>
        <!-- <el-form-item label="商品状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio  label="上架">{{ dict.label
            }}</el-radio>
                        <el-radio  label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPrizeListApi, addPrizeApi, updatePrizeApi, updatePrizeStatusApi } from "@/api/index"
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      fileType: ['png', 'jpg', 'jpeg'], // 文件类型
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        // postCode: undefined,
        // postName: undefined,
        // status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "奖品名称不能为空", trigger: "blur" }
        ],
        prizeType: [
          { required: true, message: "奖品类型不能为空", trigger: "change" }
        ],
        prizeLevel: [
          { required: true, message: "奖品等级不能为空", trigger: "change" }
        ],
        winningProbability: [
          { required: true, message: "中奖概率不能为空", trigger: "change" }
        ]
      },
      FileUploadVal: '',
      baseUrl: process.env.VUE_APP_BASE_API,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 用户状态修改
    handleStatusChange(row) {
      console.log(row, 'row');
      let text = row.status == 0 ? "停用" : "启用"
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"奖品吗？').then(function () {
        return updatePrizeStatusApi({ id: row.id, status: row.status })
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function () {
        row.status = row.status === 0 ? 1 : 0
      })
    },
    FileUploadValChange(val) {
      console.log(val, 'FileUploadValChange');
      this.FileUploadVal = val;
      this.form.imageUrl = val[0].url; // 假设只上传一个文件
    },
    /** 查询产品列表 */
    getList() {
      this.loading = true
      getPrizeListApi(this.queryParams).then(response => {
        console.log(response, 'response');
        this.postList = response.rows;
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        status: 1,
        description: undefined,
        prizeAmount: undefined,
        prizeLevel: undefined,
        prizeType: undefined,
        imageUrl: ''
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商品";
      this.FileUploadVal = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
      this.title = "修改商品";
      this.FileUploadVal = [
        {
          url: row.imageUrl,
          name: row.imageUrl.split('/').pop() // 获取文件名
        }
      ]
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePrizeApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.status = 1;
            addPrizeApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row, 'row');
      const status = row.status == 1 ? 0 : 1
      this.$modal.confirm('是否确认删除' + row.name + '？').then(function () {
        return updatePrizeStatusApi({ id: row.id, status })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
